package gy.chen;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

public class Main {
    private JButton fileChooser;
    private JTextArea infoArea;
    private JPanel rootPanel;
    private JButton cancelButton;

    private String rootPath;

    private TinyPngProcess process;

    public Main() {
        process = new TinyPngProcess();
        fileChooser.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                JFileChooser jfc = new JFileChooser();
                jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                jfc.showDialog(new JLabel(), "选择");
                File file = jfc.getSelectedFile();
                if (file.isDirectory()) {
                    rootPath = file.getAbsolutePath();
                } else if (file.isFile()) {
                    rootPath = file.getParent() + "/";
                }
                System.out.println(rootPath);
                cancelButton.setEnabled(true);
                infoArea.setText("");
                process.compress(rootPath, infoArea);
            }
        });
        cancelButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                process.cancel();
                cancelButton.setEnabled(false);
            }
        });
    }



    public static void main(String[] args) {
        JFrame frame = new JFrame("Main");
        frame.setContentPane(new Main().rootPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setSize(800,590);
    }
}
